package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.widget.ListView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.SignRecordAdapter;
import com.longcai.piaojia.conn.SignRecordGet;
import com.longcai.piaojia.util.MyToast;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/10.
 */
public class SignRecordActivity extends BaseActivity {

    @BoundView(R.id.sign_record_lv) private PullToRefreshListView pullToRefreshListView;

    private ListView sign_record_lv;

    private List<SignRecordGet.SingRecord> lists = new ArrayList();

    private SignRecordAdapter adapter;

    private SignRecordGet.SignRecordEntity signRecord;

    private SignRecordGet signRecordGet = new SignRecordGet(new AsyCallBack<SignRecordGet.SignRecordEntity>() {

        @Override
        public void onSuccess(String toast, int type, SignRecordGet.SignRecordEntity signRecordEntity) throws Exception {

            signRecord = signRecordEntity;

            lists.addAll(signRecordEntity.signRecordLists);

            adapter.notifyDataSetChanged();

            setLastUpdateTime();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.show(context, toast, MyToast.LENGTH_SHORT);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullDownRefreshComplete();
            pullToRefreshListView.onPullUpRefreshComplete();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_record);

        setBackTrue();

        setTitleName("抽奖记录");
    /*    getString(R.string.sign_record_title_text)*/
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(false);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();
                signRecordGet.page = 1;
                signRecordGet.execute(context, false);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (lists.size() != signRecord.total) {

                    signRecordGet.page = signRecord.current_page + 1;
                    signRecordGet.execute(context, false);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();

                }

            }
        });

        sign_record_lv = pullToRefreshListView.getRefreshableView();

        sign_record_lv.setDivider(null);
        sign_record_lv.setAdapter(adapter = new SignRecordAdapter(context, lists));

        signRecordGet.user_id = BaseApplication.BasePreferences.readUserId();
        signRecordGet.page = 1;
        signRecordGet.execute(this);


    }


    public void setLastUpdateTime() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("HH:mm:ss");

        String time = sDateFormat.format(System.currentTimeMillis());

        pullToRefreshListView.setLastUpdatedLabel(time);

    }

}
